import path from 'path'
import fs from 'fs'
import readzip from 'readzip';
import camelCase from 'camelcase';
import transformCode from './buildComp.js';
import pullout from 'pullout';
import { familyType, strokeWidths, ICONFAMILY, zipPaths, diffIconTypeByPath } from './constaints.js';

const filterNames = ['F7Search','F7Ellipsis','F7Plus','F7AntFill','F7HifispeakerFill','F7Cat','F7QuestionCircleFill',
    'F7ChevronDown','F7CloudUploadFill','F7GearAltFill', 'F7Speedometer', 'F7Flag',
    'F7House', 'F7LogoApple','F7Cart', 'F7DocOnDoc', 'F7GearAlt', 'F7Heart', 'F7Photo', 'F7SquarePencil',
    'F7ArrowBranch', 'F7ArrowshapeTurnUpRight', 'F7CubeBox', 'F7Gear', 'F7RocketFill', 'F7Star', 'F7TrayArrowDown',
    'F7Wrench','F7Person2Fill', 'F7PersonFill','F7LockFill','F7Tree','F7ExclamationmarkTriangleFill',
    'F7CheckmarkAltCircleFill', 'F7XmarkCircleFill', 'F7InfoCircleFill','F7Person'];

// const filterNames = ['FeatherCode','FeatherCodesandbox','FeatherCopy','FeatherChevronDown','FeatherChevronRight','FeatherChevronLeft','FeatherChevronUp','FeatherChevronsLeft','FeatherChevronsRight','FeatherChevronsDown','FeatherChevronsUp','FeatherSettings','FeatherCamera','FeatherBox','FeatherThumbsUp','FeatherLoader','FeatherLink','FeatherPlus','FeatherSearch','FeatherUser','FeatherMinus','FeatherStar','FeatherTag','FeatherArrowLeft','FeatherArrowRight','FeatherX','FeatherBell','FeatherShare','FeatherList','FeatherTool','FeatherCommand','FeatherSlack','FeatherHeart','FeatherImage','FeatherHelpCircle','FeatherFileText','FeatherCalendar','FeatherClock','FeatherEye','FeatherEyeOff','FeatherXCircle','FeatherAlertCircle','FeatherFilm','FeatherMusic','FeatherKey','FeatherMail','FeatherSmartphone','FeatherCheckCircle','FeatherAlertTriangle','FeatherCheck','FeatherMinusSquare','FeatherPlusSquare','FeatherMoreHorizontal'];
const family = ICONFAMILY.F7

const dir = `../src/icons/${family}`;
const distFile = `../doc/${family}/${family}.tsx`;
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(distFile, `
import type { JSX } from 'solid-js';
import { convertIcon, IconProps } from '../../Icon';

`, 'utf-8');

for await (const item of readzip(`../src/svgs/${zipPaths[family]}.zip`)) {
    const {name} = item;
    if (!item.isFile() || !name.endsWith('.svg')) {
        continue;
    }

    let fileName = path.basename(name, '.svg');
    // 根据路径判断图标类型
    if (diffIconTypeByPath[family]) {
        const typeMap = diffIconTypeByPath[family];
        Object.keys(typeMap).forEach(type => {
            if (item.filePath.includes(typeMap[type])) {
                fileName = fileName += `-${type}`;
            }
        })
    }
    // // let componentName = fileName.includes(type) ? fileName: fileName + '-' + type;
    const componentName = camelCase(family + '-' + fileName, {pascalCase: true});
    if (!filterNames.includes(componentName)) {
        continue;
    }
    let type = familyType[family] ?? (fileName.includes('fill')? 'filled' : fileName.includes('outline') ? 'outline' : '');
    if (family === ICONFAMILY.IONICONS) {
        type = fileName.includes('outline') ? 'custom' : 'filled';
    }
    if (family === ICONFAMILY.ANT) {
        type = fileName.includes('twotone') ? 'twotone' : type;
    }
    // // const destFileName = path.join('../src/icons', family, componentName + '.tsx');
    console.log(fileName, type, componentName);
    const fc = await item.openReadStream();
    let fileContent = await pullout(fc);

    let tsxCode = await transformCode(fileContent, fileName, componentName, type, family, strokeWidths[family])
    tsxCode = tsxCode.replace('            strokeWidth={2}\n', '')
    .replace(/strokeLinecap/g, 'stroke-linecap').replace(/strokeLinejoin/g, 'stroke-linejoin')
    .replace(/fillRule/g, 'fill-rule').replace(/clipRule/g, 'clip-rule')
    tsxCode = tsxCode.replace(/strokeMiterlimit/g, 'stroke-miterlimit')
    .replace(/strokeWidth/g, 'stroke-width').replace(/className/g, 'class').replace(/enableBackground/g, 'enable-background')
	.replace(/strokeDasharray/g, 'stroke-dasharray').replace(/xmlSpace/g, 'xml-space')
	.replace(/stopColor/g, 'stop-color')
	.replace(/fillOpacity/g, 'fill-opacity')
	.replace(/xmlnsXlink/g, 'xmlns-xlink')
    if (family === ICONFAMILY.ANT) {
        tsxCode = tsxCode.replace(/fill="#D9D9D9"/g, 'class="cui-tt-secondary"')
        .replace(/fill="#333"/g, 'class="cui-tt-primary"')
        .replace(/fill="#E6E6E6"/g, 'class="cui-tt-tertiary"')
    }

    fs.appendFileSync(distFile, tsxCode, 'utf-8');

    fs.appendFileSync(distFile, `\n\n`, 'utf-8');
}